package com.ht.medicaltreatment.activity

import android.annotation.SuppressLint
import android.content.Intent
import android.view.View
import androidx.activity.viewModels
import androidx.recyclerview.widget.LinearLayoutManager
import com.blankj.utilcode.util.ClickUtils
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.listener.OnItemClickListener
import com.gym.baselib.base.BaseActivity
import com.ht.medicaltreatment.R
import com.ht.medicaltreatment.adapter.BorrowUpdateAdapter
import com.ht.medicaltreatment.bean.LoanSample
import com.ht.medicaltreatment.view.TypeDialog
import com.ht.medicaltreatment.viewmodel.BorrowBuildViewModel
import com.xgst.lib.utils.showToast
import kotlinx.android.synthetic.main.activity_build_borrow_update_layout.*

//借出表单  修改数据
class BorrowUpdateActivity:BaseActivity(), View.OnClickListener, OnItemClickListener {
    override fun getLayoutId() = R.layout.activity_build_borrow_update_layout

    private var typeId = ""
    private val model by viewModels<BorrowBuildViewModel>()
    private var orderId = -1
    private var projectId = -1
    private lateinit var adapter:BorrowUpdateAdapter

    override fun init() {
        setTitle("修改计划表单")
        orderId = intent.getIntExtra("orderId",-1)
        showProgress()
        tvProjectName.setOnClickListener(this)
        tvName.setOnClickListener(this)
        btnStart.setOnClickListener(this)
        btnPositive.setOnClickListener(this)
        imgDelete.setOnClickListener(this)
        cbAll.setOnClickListener(this)
        tvReturnTime.setOnClickListener(this)
        tvSample.setOnClickListener(this)
        tvSampleBox.setOnClickListener(this)
        tvQuery.setOnClickListener(this)
        tvType.setOnClickListener(this)

        ClickUtils.expandClickArea(imgDelete,30)

        recyclerView.layoutManager = LinearLayoutManager(this)
        adapter = BorrowUpdateAdapter()
        recyclerView.adapter = adapter
        adapter.setOnItemClickListener(this)

        initData()
    }

    fun initData(){
        model.detailBorrow(orderId) {
            adapter.setNewInstance(it.loanSampleList)
            setTitle(it.loanOrder.orderName)
            it.loanOrder.id
            tvName.text = it.loanOrder.takeLeave
            tvProjectName.text = it.loanOrder.projectName
            tvReturnTime.text = it.loanOrder.expectedReturnDate
            etRemark.text = it.loanOrder.loanRemarks
            projectId = it.loanOrder.projectId
            etName.setText(it.loanOrder.orderName)
            tvCheckCount.text = "${it.loanSampleList.size}"
            dismissProgress()
        }
    }

    override fun isViewStup() = true

    @SuppressLint("NotifyDataSetChanged")
    override fun onClick(v: View?) {
        when(v){
            cbAll -> {
                adapter.data.forEach {
                    it.isChecked = cbAll.isChecked
                }
                adapter.notifyDataSetChanged()
            }

            //删除
            imgDelete -> {
                val list = adapter.data.filter { it.isChecked }
                if(list.isEmpty()){
                    return
                }
                showProgress()
                val sb = StringBuffer()
                list.forEach {
                    sb.append("${it.id},")
                }
                val ids = sb.toString().substring(0,sb.toString().length - 1)
                model.deleteSmaple(ids) {
                    dismissProgress()
                    if (adapter.data.removeIf { it.isChecked }) {
                        adapter.notifyDataSetChanged()
                        showToast("已删除")
                        tvCheckCount.text = "${adapter.data.size}"
                    }
                }
            }

            tvQuery -> {
                llType.visibility = View.VISIBLE
            }

            tvSample -> {
                llType.visibility = View.GONE
                tvQuery.text = "样本"
            }

            tvSampleBox->{
                llType.visibility = View.GONE
                tvQuery.text = "样本盒"
            }

            //样本类别
            tvType -> {
                TypeDialog(this).show { id, name ->
                    tvType.text = name
                    typeId = id
                }
            }

            //查询
            btnStart -> {
                val intent = Intent(this,BorrowQueryActivity::class.java)
                intent.putExtra("projectId","$projectId")
                intent.putExtra("sampleName",etSampleName.text.toString())
                intent.putExtra("sampleCategoryId",typeId)
                intent.putExtra("orderId","$orderId")
                intent.putExtra("type",if (tvQuery.text.toString() == "样本")  "0" else "1")

                startActivityForResult(intent){
                    showProgress()
                    initData()
                }
            }

            btnPositive -> {
                val intent = Intent(this,BorrowOrderDetailActivity::class.java)
                intent.putExtra("id",orderId)
                startActivity(intent)
                finish()
            }
        }
    }

    override fun onItemClick(adapter: BaseQuickAdapter<*, *>, view: View, position: Int) {
        val item = adapter.getItem(position) as LoanSample
        item.isChecked = !item.isChecked
        adapter.notifyItemChanged(position)
        val list = adapter.data as MutableList<LoanSample>
        val bean = list.find { !it.isChecked }

        if(bean == null){
            cbAll.isChecked = true
        }else{
            if(cbAll.isChecked){
                cbAll.isChecked = false
            }
        }
    }
}

